//
//  UIImageView+XMWebCache.m
//  XMKit
//
//  Created by chenming on 2017/11/8.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "UIImageView+XMWebCache.h"

@implementation UIImageView (XMWebCache)

- (void)xm_setTrimImageWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder{
    __weak typeof(self) weakSelf = self;
    __block UIActivityIndicatorView *activityIndicator;
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        if (!activityIndicator) {
            dispatch_async(dispatch_get_main_queue(), ^{
                activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
                if (self.width == self.height) {
                    activityIndicator.frame = CGRectMake((self.width - 20) / 2, (self.height - 20) / 2, 20, 20);
                } else {
                    activityIndicator.center = self.center;
                }
                [self addSubview:activityIndicator];
                [activityIndicator startAnimating];
            });
        }
    } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
            UIImage *img = [self xm_trimImageWithImage:image];
            weakSelf.image = img;
        }else{
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
            weakSelf.image = placeholder;
        }
    }];
}

-(UIImage *)xm_trimImageWithImage:(UIImage *)image{
    
    //imageView的宽高比
    CGFloat imageViewWidthHeightRatio =self.frame.size.width/self.frame.size.height;
    //屏幕分辨率
    //    CGFloat imageScale = [[UIScreen mainScreen] scale];
    
    CGFloat imageScale = 1;
    
    CGFloat imageWith = image.size.width*imageScale;
    
    CGFloat imageHeight = image.size.height*imageScale;
    
    //image的宽高比
    CGFloat imageWidthHeightRatio =imageWith/imageHeight;
    
    CGImageRef imageRef = nil;
    
    CGRect rect;
    
    if (imageWidthHeightRatio>imageViewWidthHeightRatio) {
        
        rect = CGRectMake((imageWith-imageHeight*imageViewWidthHeightRatio)/2, 0, imageHeight*imageViewWidthHeightRatio, imageHeight);
        
    }else if (imageWidthHeightRatio<imageViewWidthHeightRatio) {
        
        rect = CGRectMake(0, (imageHeight-imageWith/imageViewWidthHeightRatio)/2, imageWith, imageWith/imageViewWidthHeightRatio);
        
    }else {
        rect = CGRectMake(0, 0, imageWith, imageHeight);
    }
    
    imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *res = [UIImage imageWithCGImage:imageRef scale:imageScale orientation:UIImageOrientationUp];
    
    /**
     一定要，千万要release，否则等着内存泄露吧，稍微高清点的图一张图就是几M内存，很快App就挂了
     */
    CGImageRelease(imageRef);
    
    return res;
}

@end
